package site.wetsion.employ.server.controller;

import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import site.wetsion.employ.server.pojo.Company;
import site.wetsion.employ.server.pojo.Condition;
import site.wetsion.employ.server.pojo.Job;
import site.wetsion.employ.server.pojo.JobRemark;
import site.wetsion.employ.server.pojo.JobRequestDetail;
import site.wetsion.employ.server.pojo.User;
import site.wetsion.employ.server.service.CommentRemarkService;
import site.wetsion.employ.server.service.CompanyService;
import site.wetsion.employ.server.service.JobService;
import site.wetsion.employ.server.util.Page;
import site.wetsion.employ.server.util.Result;
import site.wetsion.employ.server.vo.JobVo;
import site.wetsion.employ.server.vo.RemarkPageVo;

@Controller
public class JobController {

	@Autowired
	private JobService jobService;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private CommentRemarkService commentService;
	
	@ResponseBody
	@PostMapping("job/add")
	public Result addJob(JobVo jobVo, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		Job job = jobVo.getJob();
		JobRequestDetail detail = jobVo.getJobDetail();
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		job.setJob_creater_id(user.getId());
		job.setGmt_create(timestamp);
		job.setGmt_modified(timestamp);
		detail.setGmt_create(timestamp);
		detail.setGmt_modified(timestamp);
		boolean rs = jobService.addJob(jobVo);
		if(rs){
			return new Result(200, "添加成功");
		}
		return new Result(400, "添加失败");
	}
	/** 查询职位*/
	@ResponseBody
	@GetMapping("job/queryJob")
	public JobVo queryJobById(BigInteger id){
		Job job = jobService.getJobById(id);
		JobRequestDetail detail = jobService.getJobDetailById(job.getJob_request_detail());
		JobVo vo = new JobVo();
		vo.setJob(job);
		vo.setJobDetail(detail);
		return vo;
	}
	/** ajax删除职位*/
	@ResponseBody
	@GetMapping("job/deleteJob")
	public Result deleteJob(BigInteger id){
		boolean rs = jobService.deleteJobById(id);
		if(rs){
			return new Result(200, "删除成功");
		}
		return new Result(400, "删除失败");
	}
	/** ajax修改职位信息*/
	@ResponseBody
	@PostMapping("job/updateJob")
	public Result updateJob(JobVo vo){
		Job job = vo.getJob();
		JobRequestDetail detail = vo.getJobDetail();
		boolean rj = jobService.updateJob(job);
		boolean rd = jobService.updateJobDetail(detail);
		if(!rj){
			return new Result(400, "职位基本信息修改失败！");
		}
		if(!rd){
			return new Result(400, "职位详情介绍修改失败！");
		}
		return new Result(200, "修改成功！");
	}
	/** 招聘页面分页显示，默认显示*/
	@GetMapping("job/list/{pageNum}")
	public String jobList(@PathVariable int pageNum, Model model){
		Page page = new Page(pageNum, 20);
		List<Job> jobList = jobService.getAllJobByPage(page);
		int count = jobService.getJobCount();
		page.setTotalRecord(count);
		model.addAttribute("page", page);
		model.addAttribute("jobList", jobList);
		return "joblist";
	}
	
	/** 搜索*/
	@GetMapping("job/search/{pageNum}")
	public String searchJobNext(@PathVariable int pageNum, 
			Condition condition, Model model){
//		if(condition.getKeyword() == "" || "".equals(condition.getKeyword())){
//			return "redirect:/job/list/1";
//		}
		Page page = new Page(pageNum, 10);
		List<Job> jobList = jobService.searchJobByCondition(page, condition);
		int count = jobService.getSearchCountByCondition(condition);
		page.setTotalRecord(count);
		model.addAttribute("jobList", jobList);
		model.addAttribute("page", page);
		model.addAttribute("keyword", condition.getKeyword());
		model.addAttribute("condition", condition);
		return "joblist";
	}
	
	@GetMapping("job/detail/{id}")
	public String jobdetail(@PathVariable BigInteger id, Model model){
		Job job = jobService.getJobById(id);
		JobRequestDetail detail = jobService.getJobDetailById(job.getJob_request_detail());
		JobVo vo = new JobVo();
		Company company = companyService.getCompanyById(job.getJob_company_id());
		vo.setJob(job);
		vo.setJobDetail(detail);
		Page page = new Page(1, 5);
		List<JobRemark> remarkList = commentService.getRemarksOfJobIdByPage(page, id);
		int count = commentService.getRemarkCountOfJob(id);
		page.setTotalRecord(count);
		model.addAttribute("jobVo", vo);
		model.addAttribute("company", company);
		model.addAttribute("remarkList", remarkList);
		model.addAttribute("page", page);
		return "jobdetail";
	}
	@ResponseBody
	@GetMapping("job/remark/{jobId}/{pageNum}")
	public RemarkPageVo ajaxShowRemarkList(
			@PathVariable("jobId") BigInteger jobId, 
			@PathVariable("pageNum") int pageNum){
		Page page = new Page(pageNum, 5);
		List<JobRemark> remarkList = commentService.getRemarksOfJobIdByPage(page, jobId);
		int count = commentService.getRemarkCountOfJob(jobId);
		page.setTotalRecord(count);
		RemarkPageVo vo = new RemarkPageVo();
		vo.setRemarkList(remarkList);
		vo.setPage(page);
		return vo;
	}
	
	@PostMapping("job/addremark")
	public String addJobRemark(JobRemark remark, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		if(user==null){
			return "redirect:/login";
		}
		remark.setRemark_agree(new BigInteger("0"));
		remark.setRemark_user_id(user.getId());
		commentService.addJobRemark(remark);
		return "redirect:/job/detail/"+remark.getRemark_job();
	}
	
	/**点赞职位评论*/
	@ResponseBody
	@GetMapping("job/remark/agree")
	public Result cancelCollectCompany(BigInteger id, HttpSession session){
		User user = (User) session.getAttribute("curr_user");
		if(user==null){
			return new Result(405, "登录失效");
		}
		boolean rs = commentService.addRemarkAgree(id);
		if(rs){
			return new Result(200, "点赞成功");
		}
		return new Result(400, "点赞失败");
	}
	
	@InitBinder("jobDetail")
	public void initBinder1(WebDataBinder binder){
		binder.setFieldDefaultPrefix("jobDetail."); 
	}
	@InitBinder("job")   
    public void initBinder2(WebDataBinder binder) {   
		binder.setFieldDefaultPrefix("job.");   
	}
	
}
